<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Bit String Types</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.2 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Data Types"
HREF="datatype.html"><LINK
REL="PREVIOUS"
TITLE="Network Address Types"
HREF="datatype-net-types.html"><LINK
REL="NEXT"
TITLE="Text Search Types"
HREF="datatype-textsearch.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-12-01T22:07:59"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.2 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Network Address Types"
HREF="datatype-net-types.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="datatype.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 8. Data Types</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Text Search Types"
HREF="datatype-textsearch.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="DATATYPE-BIT"
>8.10. Bit String Types</A
></H1
><P
>    Bit strings are strings of 1's and 0's.  They can be used to store
    or visualize bit masks.  There are two SQL bit types:
    <TT
CLASS="TYPE"
>bit(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
> and <TT
CLASS="TYPE"
>bit
    varying(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
>, where
    <TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
> is a positive integer.
   </P
><P
>    <TT
CLASS="TYPE"
>bit</TT
> type data must match the length
    <TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
> exactly; it is an error to attempt to
    store shorter or longer bit strings.  <TT
CLASS="TYPE"
>bit varying</TT
> data is
    of variable length up to the maximum length
    <TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>; longer strings will be rejected.
    Writing <TT
CLASS="TYPE"
>bit</TT
> without a length is equivalent to
    <TT
CLASS="LITERAL"
>bit(1)</TT
>, while <TT
CLASS="TYPE"
>bit varying</TT
> without a length
    specification means unlimited length.
   </P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>     If one explicitly casts a bit-string value to
     <TT
CLASS="TYPE"
>bit(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
>, it will be truncated or
     zero-padded on the right to be exactly <TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
> bits,
     without raising an error.  Similarly,
     if one explicitly casts a bit-string value to
     <TT
CLASS="TYPE"
>bit varying(<TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
>)</TT
>, it will be truncated
     on the right if it is more than <TT
CLASS="REPLACEABLE"
><I
>n</I
></TT
> bits.
    </P
></BLOCKQUOTE
></DIV
><P
>    Refer to <A
HREF="sql-syntax-lexical.html#SQL-SYNTAX-BIT-STRINGS"
>Section 4.1.2.5</A
> for information about the syntax
    of bit string constants.  Bit-logical operators and string
    manipulation functions are available; see <A
HREF="functions-bitstring.html"
>Section 9.6</A
>.
   </P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN6807"
></A
><P
><B
>Example 8-3. Using the Bit String Types</B
></P
><PRE
CLASS="PROGRAMLISTING"
>CREATE TABLE test (a BIT(3), b BIT VARYING(5));
INSERT INTO test VALUES (B'101', B'00');
INSERT INTO test VALUES (B'10', B'101');
<SAMP
CLASS="COMPUTEROUTPUT"
>ERROR:  bit string length 2 does not match type bit(3)</SAMP
>
INSERT INTO test VALUES (B'10'::bit(3), B'101');
SELECT * FROM test;
<SAMP
CLASS="COMPUTEROUTPUT"
>  a  |  b
-----+-----
 101 | 00
 100 | 101</SAMP
></PRE
></DIV
><P
>    A bit string value requires 1 byte for each group of 8 bits, plus
    5 or 8 bytes overhead depending on the length of the string
    (but long values may be compressed or moved out-of-line, as explained
    in <A
HREF="datatype-character.html"
>Section 8.3</A
> for character strings).
   </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="datatype-net-types.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="datatype-textsearch.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Network Address Types</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="datatype.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Text Search Types</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>